/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package blms.domain;

import java.io.Serializable;

/**
 * This class represents an entity that has an identity that is represented by
 * an {@link Serializable}. Its also contains identity-based method
 * implementations: {@link hashCode}, {@link equals} and {@link toString}.
 * 
 * @see Identifiable
 * 
 * @author João Arthur
 * @author Jonhnny Weslley
 * @author Thiago Emmanuel
 * @version 1.00, 25/04/2008
 * @since 1.0
 */
public abstract class Entity implements Identifiable, Serializable {

	private static final long serialVersionUID = 2620976018265522968L;

	private Serializable id;

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.domain.Identifiable#getId()
	 */
	public Serializable getId() {
		return id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.domain.Identifiable#setId(java.io.Serializable)
	 */
	public void setId(final Serializable id) {
		this.id = id;
	}

	/**
	 * Returns a hash code value for the object. This method is supported for
	 * the benefit of hash tables such as those provided by
	 * {@link java.util.Hashtable}.
	 * 
	 * @return a hash code value for this object.
	 */
	@Override
	public int hashCode() {
		return (id == null) ? 0 : id.hashCode();
	}

	/**
	 * Indicates whether other object is "equal to" this one.
	 * 
	 * @param obj -
	 *            the reference object with which to compare.
	 * @return true if this object is the same as the object argument; false
	 *         otherwise.
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Entity that = (Entity) obj;
		if (id == null) {
			if (that.id != null) {
				return false;
			}
		} else if (!id.equals(that.id)) {
			return false;
		}
		return true;
	}

	/**
	 * A String representation of this <code>Entity</code>.
	 */
	@Override
	public String toString() {
		return String.valueOf(id);
	}

}
